package com.wsc.wisdomchinaqinghe.activitity.fourmodule;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.HoveringScrollview;
import com.stx.xhb.mylibrary.XBanner;
import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.Login_RegisterActivity;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.adapter.LifeServerAdapter;
import com.wsc.wisdomchinaqinghe.application.MyApplication;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.LifeServerDetailBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyGridView;
import com.wsc.wisdomchinaqinghe.view.iphoneDialogBuilder;

import java.util.List;

public class LifeServerDetailActivity extends BaseActivity implements View.OnClickListener, ContributorView, XBanner.XBannerAdapter {

    private Context context;
    private ContributorPresenter contributorPresenter;
    private String id;
    private final String COMPANYDETAILFLAG = "COMPANYDETAILFLAG";
    private LifeServerDetailBean lifeServerDetailBean;
    private XBanner fragment_qyxq_xBanner;//轮播控件
    private TextView fragment_qyxq_title;//标题
    private TextView fragment_qyxq_JYcontent;//经营范围
    private ImageView fragment_qyxq_back;//返回按钮
    private TextView tv_yuyue;
    private TextView tv_yuyuexuzhi;
    private TextView tv_price;
    private TextView tv_star;
    private ImageView iv_one;
    private ImageView iv_two;
    private ImageView iv_three;
    private ImageView iv_four;
    private ImageView iv_five;
    private MyApplication myApplication;
    private MyGridView myGridView;
    private LinearLayout ll_mine_nav;
    private LinearLayout ll_mine_nav_replace;
    private RelativeLayout layout_nav_back;
    private HoveringScrollview refreshscrollView;
    private int layout_headTop;
    private LinearLayout ll_item;
    private TextView tv_headtitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lifeserverdetail_layout);
        context = this;
        initView();
        Intent intent=getIntent();
        id = intent.getStringExtra("id");
        contributorPresenter = new ContributorPresenter(this, context);
        contributorPresenter.getLifeServiceDetail(id, COMPANYDETAILFLAG);
        myApplication=(MyApplication) getApplication();

        refreshscrollView.setOnScrollListener(new HoveringScrollview.OnScrollListener() {
            @Override
            public void onScroll(int scrollY) {
                layout_headTop = ll_mine_nav_replace.getTop() + context.getResources().getDimensionPixelOffset(R.dimen.fivedp);;
                int commentTop = ll_item.getTop()/2;
                if (scrollY > layout_headTop) {
                    if (layout_nav_back.getParent() != ll_mine_nav) {
                        ll_mine_nav_replace.removeView(layout_nav_back);
                        ll_mine_nav.addView(layout_nav_back);
                        tv_headtitle.setText("服务详情");
                    }
                } else {

                    if (layout_nav_back.getParent() != ll_mine_nav_replace) {
                        ll_mine_nav.removeView(layout_nav_back);
                        ll_mine_nav_replace.addView(layout_nav_back);
                        tv_headtitle.setText("服务详情");
                    }
                }

                int alph = (int) (Tools.div(255, commentTop) * scrollY);
                if (alph <= 255) {
                    tv_headtitle.setTextColor(Color.argb(alph, 255, 255, 255));
                    fragment_qyxq_back.setAlpha(alph);
                    layout_nav_back.setAlpha((float) Tools.div((Tools.div(255, commentTop) * scrollY),255));
                } else {
                    tv_headtitle.setTextColor(Color.argb(255, 255, 255, 255));
                    fragment_qyxq_back.setAlpha(255);
                    layout_nav_back.setAlpha(1);
                }
            }
        });
    }

    private void initView() {
        myGridView=(MyGridView) findViewById(R.id.gridview);
        myGridView.setHaveScrollbar(false);
        fragment_qyxq_xBanner = (XBanner) findViewById(R.id.fragment_qyxq_xBanner);//轮播控件
        fragment_qyxq_title = (TextView) findViewById(R.id.fragment_qyxq_title);//标题
        fragment_qyxq_JYcontent = (TextView) findViewById(R.id.fragment_qyxq_JYcontent);//经营范围
        fragment_qyxq_back = (ImageView) findViewById(R.id.fragment_qyxq_back);//返回按钮
        tv_yuyuexuzhi=(TextView)findViewById(R.id.tv_yuyuexuzhi);
        fragment_qyxq_back.setOnClickListener(this);
        tv_yuyue=(TextView) findViewById(R.id.tv_yuyue);
        tv_yuyue.setOnClickListener(this);
        tv_price=(TextView) findViewById(R.id.tv_price);
        tv_star=(TextView) findViewById(R.id.tv_star);

        iv_one=(ImageView) findViewById(R.id.iv_one);
        iv_two=(ImageView) findViewById(R.id.iv_two);
        iv_three=(ImageView) findViewById(R.id.iv_three);
        iv_four=(ImageView) findViewById(R.id.iv_four);
        iv_five=(ImageView) findViewById(R.id.iv_five);

        fragment_qyxq_title.requestFocus();
        fragment_qyxq_title.setFocusable(true);
        fragment_qyxq_title.setFocusableInTouchMode(true);

        ll_mine_nav = (LinearLayout)findViewById(R.id.ll_mine_nav);
        ll_mine_nav_replace = (LinearLayout)findViewById(R.id.ll_mine_nav_replace);
        layout_nav_back = (RelativeLayout) findViewById(R.id.layout_nav_back);
        layout_nav_back.setAlpha(0);
        refreshscrollView = (HoveringScrollview) findViewById(R.id.mineScrollView);
        ll_item = (LinearLayout) findViewById(R.id.ll_item);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fragment_qyxq_back:
                finish();
                break;
            case R.id.tv_yuyue:
                boolean isLogin= Tools.getSharedPreferencesboolen(Constant.isLoginIn,false,context);
                if(isLogin)
                {
                    try {
                        if(lifeServerDetailBean.getTitle()!=null)
                        {
                            Intent intent=new Intent(context,LifeServerApplyActivity.class);
                            intent.putExtra("name",lifeServerDetailBean.getTitle());
                            intent.putExtra("id",lifeServerDetailBean.getId());
                            startActivity(intent);
                        }
                    }catch (Exception e)
                    {

                    }

                }else
                {
                    iphoneDialogBuilder idb = new iphoneDialogBuilder(context);
                    idb.setTitle("提示");
                    idb.setMessage("是否登录");
                    idb.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(context, Login_RegisterActivity.class);
                            myApplication.setSourceIntoLoginRegisterActivity(LifeServerDetailActivity.class.getSimpleName());
                            startActivity(intent);
                        }
                    });
                    idb.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    idb.show();
                }
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(COMPANYDETAILFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    lifeServerDetailBean = JsonTools.jsonStringToObject(jsonMessage, LifeServerDetailBean.class);
                    if (lifeServerDetailBean != null) {
                        fragment_qyxq_title.setText(lifeServerDetailBean.getTitle());
                        fragment_qyxq_JYcontent.setText(lifeServerDetailBean.getIntro());
                        tv_yuyuexuzhi.setText(lifeServerDetailBean.getContent());
                        double price=lifeServerDetailBean.getPrice();
                        if(price>0)
                        {
                            tv_price.setText(price+"元/小时");
                        }else
                        {
                            tv_price.setText("价格面议");
                        }
                        double star=lifeServerDetailBean.getStar();
                        tv_star.setText(star+"");

                        switch ((int) star)
                        {
                            case 0:
                                break;
                            case 1:
                                iv_one.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                break;
                            case 2:
                                iv_one.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                iv_two.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                break;
                            case 3:
                                iv_one.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                iv_two.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                iv_three.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                break;
                            case 4:
                                iv_one.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                iv_two.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                iv_three.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                iv_four.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                break;
                            case 5:
                                iv_one.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                iv_two.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                iv_three.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                iv_four.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                iv_five.setImageDrawable(getResources().getDrawable(R.drawable.star_1));
                                break;
                        }

                        fragment_qyxq_xBanner.setData(lifeServerDetailBean.getBanners(),null);
                        fragment_qyxq_xBanner.setPoinstPosition(XBanner.CENTER);//指示器居于中
                        fragment_qyxq_xBanner.setmAdapter(this);

                        List<String> rangeList=lifeServerDetailBean.getRangeList();

                        myGridView.setAdapter(new LifeServerAdapter(context,rangeList));
                    }

                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

    @Override
    public void loadBanner(XBanner banner, Object model, View view, int position) {
        try {
            Glide.with(context).load(lifeServerDetailBean.getBanners().get(position)).into((ImageView) view);
        }catch (Exception e)
        {

        }
    }

}
